<?php
/**
 * Created by PhpStorm.
 * User: 柳迪
 * Date: 2018/4/22
 * Time: 16:59
 */

namespace app\index\controller;
use think\Controller;
use app\index\model\Url;
use think\Db;
use think\Session;

class Singers extends Controller
{

    public function index()
    {
        return $this->fetch();
    }

    public function getSinger($sid)
    {
        $result=null;
        try{
            $result=\db('singers')->find($sid);
            $result1=\db('musics')->where('sid',$sid)->select();
            echo json_encode(['singer'=>$result,'musiclist'=>$result1]);
        }
        catch (\mysqli_sql_exception $e)
        {
            $e->getError();
        }
        die();
    }

    public function getSlideList()
    {
        $result=\db('singers')->join('musics','musics.sid=singers.sid')->group('musics.sid')->having('count(musics.mid)>0')->select();
        $con_type='';
        $i=1;
        foreach ($result as $singer)
        {
            $con_type=$con_type.'<a onclick="query_Singers_con(1,'.$singer['sid'].','.$i.')"  class="list-group-item">'.$singer['sname'].'</a>';
            $i++;
        }
        return $con_type;
    }

    public function getCon_Singers($page=1,$sid=-1)
    {
        if($page==-1)
            $page=Session::get('singers_page')+1;
        if($page==-2)
            $page=Session::get('singers_page')-1;
        $result=null;
        $con_Singers='';
        $singername="全部";
        if($sid==0)
        {
            $result=db('musics')->alias('m')->join('singers s','s.sid=m.sid')->select();
            Session::set('singerlist',$result);
            Session::set('singername',$singername);
            Session::set('singers_page',1);
        }
        else if($sid!=-1)
        {
            $result_singer=db('singers')->find($sid);
            $singername=$result_singer['sname'];
            $result=Db::query('SELECT * FROM musics join singers on singers.sid=musics.sid where musics.sid='.$sid);
            Session::set('singerlist',$result);
            Session::set('singers_page',1);
            Session::set('singername',$singername);
        }
        else if ($sid==-1 && Session::has('singerlist'))
        {
            $singername=Session::get('singername');
            $result=Session::get('singerlist');
        }
        else
        {
            $result=null;
        }
        $singername=Session::get('singername');
        $con_Singers=$con_Singers.'<a onclick="refresh()" class="pull-right text-muted m-t-lg" data-toggle="class:fa-spin"><i class="icon-refresh i-lg  inline" id="refresh"></i></a>'
            .'<h2 class="font-thin m-b">'.$singername.'</h2><div class="row row-sm">';
        if($result)
        {

            $countpage=ceil(count($result)*1.0/12);

            $result_part=array_splice($result,(($page-1)*12),12);
            foreach ($result_part as $music)
            {
                $con_Singers=$con_Singers.'<div class="col-xs-6 col-sm-4 col-md-3 col-lg-2">
                                <div class="item">
                                                    <div class="pos-rlt">
                                                        <div class="item-overlay opacity r r-2x bg-black">
                                                            <div class="center text-center m-t-n">
                                                                <a onclick="addList_play('.$music['mid'].')"><i class="fa fa-play-circle i-2x"></i></a>
                                                            </div>
                                                        </div>
                                                        <img src="'.Url::ImgUrl_256_256().$music['poster'].'" alt="" class="r r-2x img-full">
                                                    </div>
                                                    <div class="padder-v">
                                                        <a href="#" onclick="showMusic('.$music['mid'].')" class="text-ellipsis">'.$music['mname'].'</a>
                                                        <a href="#" onclick="showSinger('.$music['sid'].')"  class="text-ellipsis text-xs text-muted">'.$music['sname'].'</a>
                                                    </div>
                                                </div>
                                                </div>';
            }
            $con_Singers=$con_Singers.'</div>';
            if($countpage>1)
            {
                $con_Singers=$con_Singers.'<ul id="pages" class="pagination pagination">
                                            <li><a href="javascript:;" onclick="query_Genres_con(-2)"><i class="fa fa-chevron-left"></i></a></li>';
                for($i=0;$i<$countpage;$i++)
                {
                    $con_Singers=$con_Singers.'<li><a href="javascript:;" onclick="query_Genres_con('.($i+1).')">'.($i+1).'</a></li>';
                }
                $con_Singers=$con_Singers.'   <li><a href="javascript:;" onclick="query_Genres_con(-1)"><i class="fa fa-chevron-right"></i></a></li>
                                          </ul>';
            }
        }
        echo $con_Singers;
        die();
    }

    public function getContent()
    {
        echo '<section class="hbox stretch">
                            <!-- side content -->
                            <aside class="aside bg-light dk" id="sidebar">
                                <section class="vbox animated fadeInUp">
                                    <section class="scrollable hover">
                                        <div id="genres_slide" class="list-group no-radius no-border no-bg m-t-n-xxs m-b-none auto">
                                            <a onclick="query_Genres_con(1,0,0)" class="list-group-item active">
                                                全部
                                            </a>
                                            '.$this->getSlideList().'
                                        </div>
                                    </section>
                                </section>
                            </aside>
                            <!-- / side content -->
                            <section>
                                <section class="vbox">
                                    <section id="genres_content" class="scrollable padder-lg text-center">
                                    <a onclick="refresh()" class="pull-right text-muted m-t-lg" data-toggle="class:fa-spin">
                                    <i class="icon-refresh i-lg  inline" id="refresh"></i></a>
                                        <h2 class="font-thin m-b">Acoustic</h2>
                                        <div class="row row-sm">
                                            <!--一页十二个-->
                                        </div>
                                        <!--分页用-->
                                        <!--<ul class="pagination pagination">-->
                                            <!--<li><a href="#"><i class="fa fa-chevron-left"></i></a></li>-->
                                            <!--<li class="active"><a href="#">1</a></li>-->
                                            <!--<li><a href="#">2</a></li>-->
                                            <!--<li><a href="#">3</a></li>-->
                                            <!--<li><a href="#">4</a></li>-->
                                            <!--<li><a href="#">5</a></li>-->
                                            <!--<li><a href="#"><i class="fa fa-chevron-right"></i></a></li>-->
                                        <!--</ul>-->
                                    </section>
                                </section>
                            </section>
                        </section>';
        die();
    }

}